﻿Imports QLPhuTungOto.Data
Imports DevExpress.XtraEditors
Imports QLPhuTungOto.Bus
Imports DevExpress.XtraGrid

Public Class ucQLKhac

    Private Sub ucQLKhac_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            LoadDSLoaiPhuTung()
            LoadDSXe()
            LoadDSNhaCungCap()
            'LoadDSNguoiDung()
            'LoadDSQuyenDangNhap()
            GroupControl1.Controls.Clear()
            Dim uc As New ucDoiMatKhau
            GroupControl1.Controls.Add(uc)
            uc.Dock = DockStyle.Fill
        Catch ex As Exception

        End Try
        

    End Sub


#Region "loại phụ tùng"
    Dim LPT_BUS As New LoaiPhuTungBUS

    Private Sub LoadDSLoaiPhuTung()
        gcDSLoaiPhuTung.DataSource = LPT_BUS.LayDSLoaiPhuTung
    End Sub

    Private Sub SuaLoaiPhuTung()
        Dim loaiPhuTung As New LoaiPhuTung With {.MaLoaiPhuTung = Integer.Parse(gvDSLoaiPhuTung.GetRowCellValue(gvDSLoaiPhuTung.FocusedRowHandle, "MaLoaiPhuTung")),
                                                 .TenLoaiPhuTung = gvDSLoaiPhuTung.GetRowCellValue(gvDSLoaiPhuTung.FocusedRowHandle, "TenLoaiPhuTung")}
        LPT_BUS.CapNhatLoaiPhuTung(loaiPhuTung)
    End Sub

    Private Sub gvDSLoaiPhuTung_CellValueChanged(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles gvDSLoaiPhuTung.CellValueChanged
        Try
            If e.Value.ToString().Trim().Length = 0 Then
                MessageBox.Show("Tên loại phụ tùng không được để trống", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                LoadDSLoaiPhuTung()
            Else
                SuaLoaiPhuTung()
                TrangThai.ThayDoiLoaiPhuTung()
                LoadDSLoaiPhuTung()
            End If
        Catch ex As Exception
            MessageBox.Show("Cập nhật thông tin thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub gvDSLoaiPhuTung_FocusedRowChanged(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles gvDSLoaiPhuTung.FocusedRowChanged
        Try
            txtTenLoaiPhuTung.Text = gvDSLoaiPhuTung.GetRowCellValue(gvDSLoaiPhuTung.FocusedRowHandle, "TenLoaiPhuTung")
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnThemLoaiPhuTung_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemLoaiPhuTung.Click
        Try
            If txtTenLoaiPhuTung.Text.Trim.Length = 0 Then
                MessageBox.Show("Tên loại phụ tùng không được trống", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Else
                ThemLoaiPhuTung()
                TrangThai.ThayDoiLoaiPhuTung()
                LoadDSLoaiPhuTung()
            End If
        Catch ex As Exception
            MessageBox.Show("Không thể thêm loại phụ tùng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        
    End Sub

    Private Sub ThemLoaiPhuTung()
        Dim loaiPhuTung As New LoaiPhuTung With {.TenLoaiPhuTung = txtTenLoaiPhuTung.Text}
        LPT_BUS.ThemLoaiPhuTung(loaiPhuTung)
    End Sub

    Private Sub btnXoaLoaiPhuTung_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaLoaiPhuTung.Click


        If MessageBox.Show("Bạn thực sự muốn xóa?", "Câu hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            Try
                XoaLPT()
                TrangThai.ThayDoiLoaiPhuTung()
                LoadDSLoaiPhuTung()
            Catch ex As Exception
                MessageBox.Show("Không thể xóa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If


    End Sub

    Private Sub XoaLPT()
        Dim maLPT As Integer
        maLPT = Integer.Parse(gvDSLoaiPhuTung.GetRowCellValue(gvDSLoaiPhuTung.FocusedRowHandle, "MaLoaiPhuTung"))
        LPT_BUS.XoaLoaiPhuTung(maLPT)
    End Sub
#End Region

#Region "Xe"
    Dim Xe_BUS As New XeBUS

    Private Sub LoadDSXe()
        gcDSXe.DataSource = Xe_BUS.LayDSXe()
    End Sub

    Private Sub SuaXe()
        Dim xe As New Xe With {.MaXe = Integer.Parse(gvDSXe.GetRowCellValue(gvDSXe.FocusedRowHandle, "MaXe")),
                               .TenXe = gvDSXe.GetRowCellValue(gvDSXe.FocusedRowHandle, "TenXe")}
        Xe_BUS.SuaXe(xe)
    End Sub

    Private Sub gvDSXe_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles gvDSXe.CellValueChanged

        Try

            If e.Value.ToString.Trim.Length = 0 Then
                MessageBox.Show("Tên xe không được để trống", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                LoadDSXe()
            Else
                SuaXe()
                TrangThai.ThayDoiXe()
                LoadDSXe()
            End If
        Catch ex As Exception
            MessageBox.Show("Cập nhật thông tin thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        
    End Sub

    Private Sub gvDSXe_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles gvDSXe.FocusedRowChanged
        Try
            txtTenXe.Text = gvDSXe.GetRowCellValue(gvDSXe.FocusedRowHandle, "TenXe")
        Catch ex As Exception

        End Try

    End Sub

    Private Sub ThemXe()
        Dim xe As New Xe With {.TenXe = txtTenXe.Text}
        Xe_BUS.ThemXe(xe)
    End Sub

    Private Sub btnThemLoaiXe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemLoaiXe.Click
        Try

            If txtTenXe.Text.Trim.Length = 0 Then
                MessageBox.Show("Tên xe không được trống", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Else
                ThemXe()
                TrangThai.ThayDoiXe()
                LoadDSXe()
            End If

        Catch ex As Exception
            MessageBox.Show("Không thể thêm xe", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        
    End Sub

    Private Sub btnXoaXe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaXe.Click


        If MessageBox.Show("Bạn thực sự muốn xóa?", "Câu hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            Try
                XoaXe()
                TrangThai.ThayDoiXe()
                LoadDSXe()
            Catch ex As Exception
                MessageBox.Show("Không thể xóa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If

    End Sub

    Private Sub XoaXe()
        Dim maXe As Integer
        maXe = Integer.Parse(gvDSXe.GetRowCellValue(gvDSXe.FocusedRowHandle, "MaXe"))
        Xe_BUS.XoaXe(maXe)
    End Sub
#End Region

#Region "nhà cung cấp"

    Dim NCC_BUS As New NhaCungCapBUS
    Private Sub LoadDSNhaCungCap()
        gcDSNhaCungCap.DataSource = NCC_BUS.LayDSNhaCungCap()
    End Sub

    Public Sub SuaNCC()
        Dim ncc As New NhaCungCap With {.MaNhaCungCap = Integer.Parse(gvDSNhaCungCap.GetRowCellValue(gvDSNhaCungCap.FocusedRowHandle, "MaNhaCungCap")),
                                        .TenNhaCungCap = gvDSNhaCungCap.GetRowCellValue(gvDSNhaCungCap.FocusedRowHandle, "TenNhaCungCap"),
                                        .Sdt = gvDSNhaCungCap.GetRowCellValue(gvDSNhaCungCap.FocusedRowHandle, "Sdt"),
                                        .Fax = gvDSNhaCungCap.GetRowCellValue(gvDSNhaCungCap.FocusedRowHandle, "Fax"),
                                        .DiaChi = gvDSNhaCungCap.GetRowCellValue(gvDSNhaCungCap.FocusedRowHandle, "DiaChi"),
                                        .ThongTinKhac = gvDSNhaCungCap.GetRowCellValue(gvDSNhaCungCap.FocusedRowHandle, "ThongTinKhac")}
        NCC_BUS.SuaNhaCungCap(ncc)
    End Sub

    Private Sub gvDSNhaCungCap_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles gvDSNhaCungCap.CellValueChanged
        Try

            If gvDSNhaCungCap.GetRowCellValue(gvDSNhaCungCap.FocusedRowHandle, "TenNhaCungCap").ToString.Trim.Length = 0 Then
                MessageBox.Show("Tên nhà cung cấp không được để trống", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                LoadDSNhaCungCap()
            Else
                SuaNCC()
                TrangThai.ThayDoiNhaCungCap()
                LoadDSNhaCungCap()
            End If

            
        Catch ex As Exception
            MessageBox.Show("Cập nhật thông tin thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub gvDSNhaCungCap_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles gvDSNhaCungCap.FocusedRowChanged

        Try
            txtTenNhaCungCap.Text = gvDSNhaCungCap.GetRowCellValue(gvDSNhaCungCap.FocusedRowHandle, "TenNhaCungCap")
            txtSDT.Text = gvDSNhaCungCap.GetRowCellValue(gvDSNhaCungCap.FocusedRowHandle, "Sdt")
            txtFax.Text = gvDSNhaCungCap.GetRowCellValue(gvDSNhaCungCap.FocusedRowHandle, "Fax")
            txtDiaChi.Text = gvDSNhaCungCap.GetRowCellValue(gvDSNhaCungCap.FocusedRowHandle, "DiaChi")
            txtThongTinKhac.Text = gvDSNhaCungCap.GetRowCellValue(gvDSNhaCungCap.FocusedRowHandle, "ThongTinKhac")
        Catch ex As Exception

        End Try
        
    End Sub

    Private Sub ThemNCC()
        Dim ncc As New NhaCungCap With {.TenNhaCungCap = txtTenNhaCungCap.Text,
                                        .Sdt = txtSDT.Text,
                                        .Fax = txtFax.Text,
                                        .DiaChi = txtDiaChi.Text,
                                        .ThongTinKhac = txtThongTinKhac.Text}
        NCC_BUS.ThemNhaCungCap(ncc)
    End Sub

    Private Sub btnThemNhaCungCap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemNhaCungCap.Click
        Try

            If txtTenNhaCungCap.Text.Trim.Length = 0 Then
                MessageBox.Show("Tên nhà cung cấp không được trống", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Else
                If NCC_BUS.LayNhaCungCapTheoTen(txtTenNhaCungCap.Text).TenNhaCungCap = txtTenNhaCungCap.Text Then
                    If MessageBox.Show("Tên nhà cung cấp đã tồn tại." + ControlChars.NewLine + "Bạn có muốn tiếp tục không?", "Câu hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) _
                        = DialogResult.Yes Then
                        ThemNCC()
                        TrangThai.ThayDoiNhaCungCap()
                        LoadDSNhaCungCap()
                    End If
                Else
                    ThemNCC()
                    TrangThai.ThayDoiNhaCungCap()
                    LoadDSNhaCungCap()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("Không thể thêm nhà cung cấp", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub btnXoaNCC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaNCC.Click


        If MessageBox.Show("Bạn thực sự muốn xóa?", "Câu hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            Try
                XoaNCC()
                TrangThai.ThayDoiNhaCungCap()
                LoadDSNhaCungCap()
            Catch ex As Exception
                MessageBox.Show("Không thể xóa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If


    End Sub

    Private Sub XoaNCC()
        Dim maNCC As Integer
        maNCC = Integer.Parse(gvDSNhaCungCap.GetRowCellValue(gvDSNhaCungCap.FocusedRowHandle, "MaNhaCungCap"))
        NCC_BUS.XoaNhaCungCap(maNCC)
    End Sub
#End Region

#Region "người dùng"
    Dim ND_BUS As New NguoiDungBUS
    Dim QDN_BUS As New QuyenDangNhapBUS

    Private Sub LoadDSNguoiDung()
        gcDSNguoiDung.DataSource = ND_BUS.LayDSNguoiDung()
    End Sub

    Private Sub LoadDSQuyenDangNhap()
        cbxQuyenDangNhap.DataSource = QDN_BUS.LayDSQuyenDangNhap()
        cbxQuyenDangNhap.ValueMember = "MaQuyenDangNhap"
        cbxQuyenDangNhap.DisplayMember = "TenQuyenDangNhap"
    End Sub

    Private Sub gvDSNguoiDung_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles gvDSNguoiDung.FocusedRowChanged
        txtTenDangNhap.Text = gvDSNguoiDung.GetRowCellValue(gvDSNguoiDung.FocusedRowHandle, "TenDangNhap")
        cbxQuyenDangNhap.SelectedValue = gvDSNguoiDung.GetRowCellValue(gvDSNguoiDung.FocusedRowHandle, "MaQuyenDangNhap")

        If Integer.Parse(gvDSNguoiDung.GetRowCellValue(gvDSNguoiDung.FocusedRowHandle, "TinhTrang")) = 1 Then
            rdoHoatDong.Checked = True
        Else
            rdoKhoa.Checked = True
        End If

    End Sub

    Private Sub XoaNguoiDung()
        ND_BUS.XoaNguoiDung(gvDSNguoiDung.GetRowCellValue(gvDSNguoiDung.FocusedRowHandle, "MaNguoiDung"))
    End Sub
    Private Sub btnXoaTaiKhoan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaTaiKhoan.Click
        XoaNguoiDung()
        LoadDSNguoiDung()
    End Sub

    Public Sub SuaNguoiDung()
        Dim tinhtrang As Integer = 0
        If rdoHoatDong.Checked = True Then
            tinhtrang = 1
        End If

        Dim nd As New NguoiDung With {.MaNguoiDung = Integer.Parse(gvDSNguoiDung.GetRowCellValue(gvDSNguoiDung.FocusedRowHandle, "MaNguoiDung")),
                                      .MaQuyenDangNhap = cbxQuyenDangNhap.SelectedValue,
                                      .TinhTrang = tinhtrang}

        ND_BUS.SuaNguoiDung(nd)
    End Sub
    Private Sub btnCapNhatTaiKhoan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhatTaiKhoan.Click
        SuaNguoiDung()
        LoadDSNguoiDung()
    End Sub
#End Region

    Private Sub LabelControl3_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles LabelControl3.Paint
        Try

            If TrangThai.ThayDoiLoaiPhuTung_Khac Then
                LoadDSLoaiPhuTung()
                TrangThai.ThayDoiLoaiPhuTung_Khac = False
            End If

            If TrangThai.ThayDoiXe_Khac Then
                LoadDSXe()
                TrangThai.ThayDoiXe_Khac = False
            End If

            If TrangThai.ThayDoiNhaCungCap_Khac Then
                LoadDSNhaCungCap()
                TrangThai.ThayDoiNhaCungCap_Khac = False
            End If

        Catch ex As Exception

        End Try
    End Sub
End Class
